package cc.telecomdigital.tdstock.Framework.UniqueItem;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

public class UnLockScreenListener extends BroadcastReceiver
{
	// For external usage
	public interface IUnLockScreenListener
	{
		/** Report on/off for the current display status **/
		void UnLockScreen_Response();
	}

	private boolean					debugMode		= false;
	private final String				LOG_TAG	= "UnLockScreenListener";

	private Context					hostContext;
	private IUnLockScreenListener	          interfaceReport;

	/** Create a Display On/Off Change Listener, Broadcast Receiver **/
	public UnLockScreenListener(Context iHostContext,
			IUnLockScreenListener iReportInterface)
	{
		if (debugMode)
			TDStockLog.d(LOG_TAG, "Create a New Receiver");

		hostContext = iHostContext;
		interfaceReport = iReportInterface;

		IntentFilter actionFilter;

		actionFilter = new IntentFilter();
		actionFilter.addAction(Intent.ACTION_USER_PRESENT);
		
		// *** Very interesting, the one would not send
		hostContext.registerReceiver(this, actionFilter);
		if (debugMode)
			TDStockLog.d(LOG_TAG, "Register Receiver");		
		
	}
	/** Disable the current usage of the Broadcast receiver **/
	private void Disable()
	{
		hostContext.unregisterReceiver(this);
		if (debugMode)
			TDStockLog.d(LOG_TAG, "Unregister Receiver");
	}

	/** Dispose the object **/
	public void Dispose()
	{
		Disable();
		if (debugMode)
			TDStockLog.d(LOG_TAG, "Object has been free");
	}

	@Override
	public void onReceive(Context context, Intent iIntent)
	{
		if (debugMode)
			TDStockLog.i(LOG_TAG, "Action: "
					+ iIntent.getAction().toString());

		if (iIntent.getAction().equals(Intent.ACTION_USER_PRESENT))
		{
			if (interfaceReport != null)
				interfaceReport.UnLockScreen_Response();
			return;
		}
	}

	/** Set DebugMessage Level **/
	public void SetDebugMode(boolean iEnable)
	{
		debugMode = iEnable;
	}
}
